package com.eagle.epay.controller;


import com.eagle.epay.common.ResultVO;
import com.eagle.epay.constant.EPayConstant;
import com.eagle.epay.entity.User;
import com.eagle.epay.form.CartAddForm;
import com.eagle.epay.form.ShippingForm;
import com.eagle.epay.service.IShippingService;
import com.eagle.epay.util.ResultVOUtil;
import com.eagle.epay.vo.CartVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author eagle
 * @since 2019-12-21
 */
@RestController
@RequestMapping("/shipping")
public class ShippingController {
	@Autowired
	IShippingService shippingService;
	@PostMapping("/add")
	public ResultVO<CartVo> add(@Valid @RequestBody ShippingForm shippingForm,
								HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		Integer result = shippingService.add(curUser.getId(), shippingForm);
		return ResultVOUtil.success(result);
	}

	@PostMapping("/update")
	public ResultVO update(@Valid @RequestBody ShippingForm shippingForm,
								HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		shippingService.update(curUser.getId(), shippingForm);
		return ResultVOUtil.success();
	}

	@GetMapping("/delete")
	public ResultVO delete(@RequestParam("shippingId") Integer shippingId,
								   HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		shippingService.delete(curUser.getId(), shippingId);
		return ResultVOUtil.success();
	}

	@GetMapping("/list")
	public ResultVO<PageInfo> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
								   @RequestParam(required = false, defaultValue = "10") Integer pageSize,
								   HttpSession session) {
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		PageInfo list = shippingService.list(curUser.getId(), pageNum, pageSize);
		return ResultVOUtil.success(list);
	}

}

